package com.android.applist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;
import android.widget.Toast;

public class AppList extends ListActivity implements ViewBinder{
	private static final String LIST_TEXT = "list_text";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.app_list);
        
        // get installed apps from the device
        PackageManager pm = this.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        List apps = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);  
        Iterator i = apps.iterator();
        
        // list of app names only
         // initListNameOnly(pm, i);
       
        // list of app names and icons
       initList(pm, i);

       ListView lv = getListView();
       lv.setTextFilterEnabled(true);

//       lv.setOnItemClickListener(new OnItemClickListener() {
//         public void onItemClick(AdapterView<?> parent, View view,
//             int position, long id) {
//           // When clicked, show a toast with the TextView text
//           Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
//               Toast.LENGTH_SHORT).show();
//         }
//       });


    }
    
    private void initListNameOnly(PackageManager pm, Iterator i) {
    	ResolveInfo rInfo; 
    	List appListData = new ArrayList<String>();
         while (i.hasNext()) {
           	rInfo = (ResolveInfo) i.next(); 
         	appListData.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
         }     

         setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, appListData));
    }
  
    private void initList(PackageManager pm, Iterator i) {
    	ResolveInfo rInfo; 
    	Map<String, ListItem> itemData;
    	
    	List<Map<String, ListItem>> listData = new ArrayList<Map<String, ListItem>>();

    	while (i.hasNext()) {
    		rInfo = (ResolveInfo) i.next(); 
    		itemData = new HashMap<String, ListItem>(1);
    		ListItem listItem1 = new ListItem();
    	    listItem1.text = rInfo.activityInfo.applicationInfo.loadLabel(pm).toString();
    	    listItem1.icon = rInfo.activityInfo.applicationInfo.loadIcon(pm);
    	    itemData.put(LIST_TEXT, listItem1);
    	    listData.add(itemData);
        }     

        SimpleAdapter simpleAdapter = new SimpleAdapter(this, listData, R.layout.list_item,
                new String[] {
                    LIST_TEXT
                }, new int[] {
                    R.id.list_text
                });
        simpleAdapter.setViewBinder(this);
        setListAdapter(simpleAdapter);
    }

    @Override
    public boolean setViewValue(View view, Object data, String stringRepresetation) {
        ListItem listItem = (ListItem) data;

        TextView menuItemView = (TextView) view;
        menuItemView.setText(listItem.text);
        menuItemView.setCompoundDrawablesWithIntrinsicBounds(listItem.icon, null, null, null);

        return true;
    }

    private class ListItem {
        public String text;
        public Drawable icon;
    }
}